<div class="row-fluid">
    <div class="span6" style="min-height: 350px">
        <div class="portlet box blue">
            <div class="portlet-title">
                <div class="caption"><i class="icon-reorder"></i>Орлогын сагс</div>
                <div class="tools">
                    <a href="javascript:;" class="collapse"></a>
                </div>
            </div>
            <div class="portlet-body form">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th width="70px"></th>
                            <th>#</th>
                            <th>Ангилал</th>
                            <th>Бараа</th>
                            <th width="80px"></th>
                            <th width="50px">Тоо(ш)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $i = 0; ?>
                        <?php $total = 0; ?>
                        <?php foreach ($userItems as $item): ?>
                            <tr>
                                <td>
                                    <a class="btn mini red" onclick="return confirm('Та устгахдаа итгэлтэй байна уу?');" href="<?php echo url_for('@income_basket_item_delete?id=' . $item['id']); ?>"><i class="icon-trash"> Устгах</i></a>
                                </td>
                                <td><?php echo ++$i; ?></td>
                                <td><?php echo $item['category_name'] ?></td>
                                <td><?php echo $item['product_name'] ?></td>
                                <td>
                                    <?php if ($item['main_image_name']): ?>
                                        <img src="<?php echo AppTools::getFullUrl() . '/uploads' . AppTools::PRODUCT_IMAGE_PATH_THUMB . $item['main_image_name'] ?>"/>
                                    <?php elseif ($item['image_name']): ?>
                                        <img src="<?php echo AppTools::getFullUrl() . '/uploads' . AppTools::PRODUCT_IMAGE_PATH_THUMB . $item['image_name'] ?>"/>
                                    <?php else: ?>

                                    <?php endif; ?>
                                </td>
                                <td><?php echo $item['quantity']; ?></td>
                                <?php $total += $item['quantity']; ?>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="5"></td>
                            <td><?php echo $total; ?></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>

    <div class="span6">
        <div class="portlet box grey">
            <div class="portlet-title">
                <div class="caption"><i class="icon-reorder"></i>Бараа нэмэх</div>
                <div class="tools">
                    <a href="javascript:;" class="collapse"></a>
                </div>
            </div>
            <div class="portlet-body form">
                <form action="<?php echo url_for('@income_basket_item_add') ?>" class="form-horizontal" method="POST" enctype="multipart/form-data">
                    <div class="control-group">
                        <label class="control-label" for="category">Ангилал:</label>
                        <div class="controls">
                            <select name="category" id="category" class="m-wrap">
                                <option value="0">[Сонгох]</option>
                                <?php foreach ($categories as $i => $v): ?>
                                    <option value="<?php echo $i ?>" <?php echo $i == $categoryId ? 'selected="selected"' : '' ?>><?php echo $v ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="product">Бараа:</label>
                        <div class="controls">
                            <select name="product" id="product" class="m-wrap">
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="quantity">Тоо(ш):</label>
                        <div class="controls">
                            <input type="text" name="quantity" id="quantity" class="m-wrap">
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn black"><i class="icon-plus"></i> Нэмэх</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="span6 float-right">
        <div class="portlet box green">
            <div class="portlet-title">
                <div class="caption"><i class="icon-reorder"></i>Орлого дуусгах</div>
                <div class="tools">
                    <a href="javascript:;" class="collapse"></a>
                </div>
            </div>
            <div class="portlet-body form">
                <form action="<?php echo url_for('@income_add') ?>" class="form-horizontal" method="POST" enctype="multipart/form-data">
                    <div class="control-group">
                        <label class="control-label" for="income_date">Орлогын огноо:</label>
                        <div class="controls">
                            <input type="text" name="income_date" id="income_date" class="m-wrap input-append date datepicker" data-date-format="yyyy-mm-dd" data-date-viewmode="years" readonly="true" value="<?php echo $incomeDate ?>">
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="trans_type">Хэнээс:</label>
                        <div class="controls">
                            <select name="trans_type" id="trans_type" class="m-wrap">
                                <?php foreach ($transactionTypes as $i => $v): ?>
                                    <option value="<?php echo $i ?>" <?php echo $i == $transactionTypeId ? 'selected="selected"' : '' ?>><?php echo $v ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>

                    <div id="trans_ent_div" class="control-group hidden">
                        <label class="control-label" for="trans_ent_id"></label>
                        <div class="controls">
                            <select name="trans_ent_id" id="trans_ent_id" class="m-wrap">
                            </select>
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn green"><i class="icon-ok"></i> Дуусгах</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    function categoryChanged() {
        c = $(this).val();
        if (c > 0) {
            $.ajax({
                url: '<?php echo $urlProduct ?>?category_id=' + c,
                success: function(d) {
                    values = $.parseJSON(d);
                    if ($.isEmptyObject(values)) {
                        $('#product').html('<option value="0">[Ангилалд бараа алга]</option>');
                    } else {
                        $('#product').html('');
                        $.each(values, function(i, v) {
                            if (i == <?php echo $productId ?>) {
                                $('#product').append('<option value="' + i + '" selected="selected">' + v + '</option>');
                            } else {
                                $('#product').append('<option value="' + i + '">' + v + '</option>');
                            }
                        });
                    }
                }
            });
        } else {
            $('#product').html('<option value="0">[Ангилал сонгоно уу]</option>');
        }
    }

    function transTypeChanged() {
        c = $(this).val();
        if (c > 0) {
            var ajaxUrl = '';
            var labelName = '';
            if (c == <?php echo TransactionTable::TRANS_TYPE_CUSTOMER ?>) {
                labelName = 'Үйлчлүүлэгч';
                ajaxUrl = '<?php echo $urlCustomer ?>';
            }
            else if (c == <?php echo TransactionTable::TRANS_TYPE_MOTION ?>) {
                labelName = 'Ажилтан';
                ajaxUrl = '<?php echo $urlUser ?>';
            }
            else if (c == <?php echo TransactionTable::TRANS_TYPE_SUPPLIER ?>) {
                labelName = 'Үйлдвэрлэгч';
                ajaxUrl = '<?php echo $urlManufacturer ?>';
            }

            if (ajaxUrl != '') {
                $.ajax({
                    url: ajaxUrl,
                    success: function(d) {
                        values = $.parseJSON(d);
                        if ($.isEmptyObject(values)) {
                            $('#trans_ent_div').addClass('hidden');
                            $('#trans_ent_div label').html('');
                            $('#trans_ent_id').html('<option value="0">NONE</option>');
                        } else {
                            $('#trans_ent_div label').html(labelName);
                            if (c == <?php echo TransactionTable::TRANS_TYPE_CUSTOMER ?>) {
                                $('#trans_ent_id').html('<option value="0">Бусад</option>');
                            } else {
                                $('#trans_ent_id').html('<option value="0">[Сонгох]</option>');
                            }
                            $.each(values, function(i, v) {
                                if (i == <?php echo $transactionEntityId ?>) {
                                    $('#trans_ent_id').append('<option value="' + i + '" selected="selected">' + v + '</option>');
                                } else {
                                    $('#trans_ent_id').append('<option value="' + i + '">' + v + '</option>');
                                }
                            });
                            $('#trans_ent_div').removeClass('hidden');
                        }
                    }
                });
            }
        } else {
            $('#trans_ent_div').addClass('hidden');
            $('#trans_ent_div label').html('');
            $('#trans_ent_id').html('<option value="0">NONE</option>');
        }
    }

    $(function() {
        $('#category').change(categoryChanged);
        $('#category').change();
        $('#trans_type').change(transTypeChanged);
        $('#trans_type').change();
    });
</script>